package com.sunye.mongodb;

/**
 * @author sunye
 * @date 2023/5/30--14:40
 */
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class MongoDbPageable implements Pageable {
    /**分页码*/
    private int pageNumber = 1;
    /**每页大小*/
    private int pageSize = 10;
    /**排序*/
    private Sort sort;

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    public void setCurrentPage(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    @Override
    public long getOffset() {
        return (pageNumber - 1) * pageSize;
    }

    @Override
    public Pageable first() {
        return PageRequest.of(0, pageSize, sort);
    }

    @Override
    public boolean hasPrevious() {
        return false;
    }

    @Override
    public Pageable next() {
        return PageRequest.of(pageNumber + 1, pageSize, sort);
    }

    @Override
    public Pageable previousOrFirst() {
        return pageNumber == 0 ? this : PageRequest.of(pageNumber - 1, pageSize, sort);
    }
}
